package com.gk.javaservice.util.meeting;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class DockerFileManager {

    /**
     * 删除Docker容器内指定路径下的文件。
     *
     * @param containerId 容器ID或名称
     * @param filePath 文件在容器内的路径
     * @return 返回操作结果的信息
     */
    public static String deleteFileInContainer(String containerId, String filePath) {
        StringBuilder result = new StringBuilder();
        try {
            // 执行docker exec命令来删除文件
            Process process = Runtime.getRuntime().exec(new String[]{
                    "sudo","docker", "exec", containerId, "rm", "-f", filePath
            });

            // 读取命令的标准输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }

            // 等待命令执行完成并获取退出码
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                result.append("文件删除成功！");
            } else {
                result.append("删除文件时出错，退出码：").append(exitCode);
            }
        } catch (Exception e) {
            result.append("执行过程中出错：").append(e.getMessage());
        }
        return result.toString();
    }

    public static void main(String[] args) {
        // 示例调用
        String containerId = "ovmedia4"; // 替换为你的容器ID或名称
        String filePath = "/opt/ovmedia/www/record/591344556/591344556_Administrator_1742199350867.mp4"; // 替换为目标文件路径

        String operationResult = deleteFileInContainer(containerId, filePath);
        System.out.println(operationResult);
    }

}
